package com.weixin.paleituo.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.weixin.paleituo.R;
import com.weixin.paleituo.bean.SharedHouseBean;
import com.weixin.paleituo.eventbus.SharedCityEvent;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.RoundCircleTransform;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 共享家样板间城市列表Adapter
 *
 * @author fengzhen
 * @version v1.0, 2018/4/23
 */
public class TemplateCityRecyclerAdapter extends RecyclerView.Adapter {
    private List<SharedHouseBean> mDataList;
    private Context mContext;
    // 选中项
    private int mSelectedPositon = -1;

    public TemplateCityRecyclerAdapter(List<SharedHouseBean> dataList) {
        this.mDataList = dataList;
    }

    public int getSelectedPositon() {
        return mSelectedPositon;
    }

    public void refreshSelectedPosition(int selectedPositon){
        int older = mSelectedPositon;
        mSelectedPositon = selectedPositon;
        notifyItemChanged(older);
        notifyItemChanged(mSelectedPositon);
    }

    public void getCityPositon(int selectedPositon) {
        if (selectedPositon < getItemCount()) {
            refreshItem(selectedPositon);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        View inflate = UIUtils.inflate(parent.getContext(), R.layout.item_city_template_house, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        final SharedHouseBean cityBean = mDataList.get(position);

        Glide.with(mContext)
                .load(cityBean.getSharedurl())
                .asBitmap()
                .error(R.mipmap.buffer)
                .transform(new RoundCircleTransform(mContext, 2))
                .placeholder(R.mipmap.buffer)
                .into(viewHolder.mImgItemCityTemplateHouse);
        viewHolder.mTvItemCityTemplateHouse.setText(cityBean.getShortName());
        viewHolder.mLinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSelectedPositon != holder.getAdapterPosition()) {
                    refreshItem(holder.getAdapterPosition());
                }
            }
        });
        if (position == mSelectedPositon) {
            viewHolder.mTvItemCityTemplateHouse.setTextColor(
                    ContextCompat.getColor(mContext, R.color.c_29CE7E));
        } else {
            viewHolder.mTvItemCityTemplateHouse.setTextColor(
                    ContextCompat.getColor(mContext, R.color.c_383838));
        }
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    /**
     * 刷新选中位置
     */
    private void refreshItem(int selectedPositon){
//        int older = mSelectedPositon;
//        mSelectedPositon = selectedPositon;
//        notifyItemChanged(older);
//        notifyItemChanged(mSelectedPositon);
        EventBus.getDefault().post(new SharedCityEvent(selectedPositon, mDataList.get(selectedPositon)));
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_item_city_template_house)
        ImageView mImgItemCityTemplateHouse;
        @BindView(R.id.tv_item_city_template_house)
        TextView mTvItemCityTemplateHouse;
        @BindView(R.id.linear_item_city_template_house)
        LinearLayout mLinear;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
